package panda.android.notebook.view;

import panda.android.notebook.R;
import panda.android.notebook.Utils.AppConfig;
import panda.android.notebook.dialog.LuyinBoFDialog;
import panda.android.notebook.dialog.PicDialog;
import panda.android.notebook.dialog.VideoDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;

public class WordModel {

	protected Bitmap bitmap;
	protected int x, y, width, height;
	public static final int WORD = 0;

	public static final int PIC = 1;
	public static final int LUYIN = 2;
	public static final int VIDEO = 3;

	private int type = WORD;

	private String path = "";
	protected Context mContext;

	private Paint mPaint;

	public WordModel(Context context, int ResourcesType) {
		// TODO Auto-generated constructor stub
		mContext = context;

		setType(ResourcesType);
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		int id = R.drawable.ic_launcher;
		switch (ResourcesType) {
		case LUYIN:
			id = R.drawable.handwrite_music;
			break;
		case PIC:
			id = R.drawable.handwrite_pic;
			break;
		case VIDEO:
			id = R.drawable.handwrite_movie;
			break;
		default:
			break;
		}
		Bitmap bm = BitmapFactory.decodeResource(context.getResources(), id);

		height = (int) (AppConfig.ROWHEIGHT - 5);
		width = (int) (bm.getWidth() / (bm.getHeight() / (float) height));

		bitmap = BitmapUtils.zoomBitmap(bm, width, (int) AppConfig.ROWHEIGHT);
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public void Draw(Canvas canvas, Paint paint) {
		if (bitmap != null)
			canvas.drawBitmap(bitmap, x, y, paint);
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public Bitmap getBitmap() {
		return bitmap;
	}

	public void setBitmap(Bitmap bitmap) {
		this.bitmap = bitmap;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	private float touchDownX, touchDownY;
	private boolean isTouch = false;

	public void onTouchEvent(MotionEvent event, ImageEditView editView) {
		// TODO Auto-generated method stub
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			touchDownX = event.getX();
			touchDownY = event.getY() + editView.getScrollY();

			if (touchDownX > x && touchDownX < x + width && touchDownY > y
					&& touchDownY < y + height) {
				isTouch = true;
			} else {
				isTouch = false;
			}
			break;
		case MotionEvent.ACTION_UP:
			touchDownX = event.getX();
			touchDownY = event.getY() + editView.getScrollY();
			if (touchDownX > x && touchDownX < x + width && touchDownY > y
					&& touchDownY < y + height) {
				System.out.println("按下"+getPath());
				if (isTouch) {
					if (getType() == WordModel.PIC)
						new PicDialog(mContext, R.style.dialog, getPath())
								.show();
					if (getType() == WordModel.LUYIN)
						new LuyinBoFDialog(mContext, R.style.dialog, getPath())
								.show();
					if (getType() == WordModel.VIDEO)
						new VideoDialog(mContext, R.style.dialog, getPath())
								.show();
				}

			}
			isTouch = false;

			break;
		default:
			break;
		}
	}
}
